package com.tttshaoqi.server.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttshaoqi.common.result.PageResult;
import com.tttshaoqi.common.result.Result;
import com.tttshaoqi.pojo.entity.Flight;
import com.tttshaoqi.pojo.entity.Order;
import com.tttshaoqi.pojo.vo.OrderVO;
import com.tttshaoqi.server.mapper.FlightMapper;
import com.tttshaoqi.server.mapper.OrderMapper;
import com.tttshaoqi.server.service.FlightService;

import com.tttshaoqi.server.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class FlightServiceImpl implements FlightService {

    @Autowired
    private FlightMapper flightMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 分页查询商品列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult page(String airlineName, Integer page, Integer pageSize, String departureCity, String arrivalCity) {
        PageHelper.startPage(page,pageSize);
        List<Flight> list = flightMapper.getList(airlineName,departureCity,arrivalCity) ;
        Page<Flight> p = (Page<Flight>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 修改航班信息
     * @param flight
     */
    @Override
    public void update(Flight flight) {
        double dis = flight.getDiscount();
        if(dis<10) dis*=10;
        double k = dis/=100;
        BigDecimal salePrice = flight.getPrice().multiply(BigDecimal.valueOf(k));
        log.info("salePrice : {}",salePrice);
        flight.setSalePrice(salePrice);
        flightMapper.update(flight);
    }

    /**
     * 添加航班信息
     * @param flight
     */
    @Override
    public void add(Flight flight) {
        double dis = flight.getDiscount();
        if(dis<10) dis*=10;
        double k = dis/=100;
        BigDecimal salePrice = flight.getPrice().multiply(BigDecimal.valueOf(k));
        flight.setSalePrice(salePrice);
        flightMapper.add(flight);
    }

    /**
     * 删除航班信息
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        List<Order> list = orderMapper.getByFID(id);
        if(!list.isEmpty()){
            return Result.error("该航班存在订单，删除失败！");
        }
        flightMapper.deleteById(id);
        return Result.success();
    }
}
